<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh" success-text="刷新成功">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
        offset="50"
        >
        <ArticleItem
        v-for="obj in list" :key="obj.art_id" :artObj='obj' @disLikeEV="disLikeFn" @reportEV='reportFn'
        @click.native="itemClickFn(obj.art_id)"
        ></ArticleItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import ArticleItem from '../../../components/ArticleItem.vue'
import {
  getAllArticleListAPI, dislikeArticleAPI,
  reportArticleAPI
} from '@/api'
import { Toast } from 'vant'
export default {
  props: {
    // list: Array
    channelId: Number
  },
  data () {
    return {
      list: [], // 文章列表数组
      loading: false, // 底部加载状态
      finished: false, // 底部完成状态
      theTime: new Date().getTime(), // 分页
      isLoading: false // 顶部加载状态
    }
  },
  components: {
    ArticleItem
  },
  created () {
    this.getArticleListFn()
  },
  methods: {
    // 专门负责发送请求
    async getArticleListFn () {
      const res = await getAllArticleListAPI({
        channel_id: this.channelId,
        timestamp: this.theTime
      })
      console.log(res)
      this.list = [...this.list, ...res.data.data.results]

      this.theTime = res.data.data.pre_timestamp
      this.loading = false
      if (res.data.data.results.length === null) {
        this.finished = true
      }
      // 顶部加载状态为 false
      this.isLoading = false
    },

    // 底部加载的事件方法
    onLoad () {
      if (this.list.length === 0) {
        this.loading = false
        return // 如果页面没有数据没有高度，让本次onLoad 逻辑不往下执行
      }
      this.getArticleListFn()
    },

    // 顶部刷新的事件方法
    onRefresh () {
      this.list = [] // list 数组清空,来一份新的数据
      this.theTime = new Date().getTime()
      this.getArticleListFn()
    },

    // 反馈 不感兴趣
    async disLikeFn (id) {
      await dislikeArticleAPI({
        artId: id
      })
      Toast.success('反馈成功')
    },
    // 反馈 垃圾内容
    async reportFn (id, value) {
      await reportArticleAPI({
        artId: id,
        type: value
      })
      Toast.success('举报成功')
    },
    itemClickFn (id) {
      this.$router.push({
        path: `/detail?art_id=${id}`
      })
    }
  }
}
</script>

<style>

</style>
